package com.siao.core
{
	import com.siao.errors.SingletonError;
	
	import flash.globalization.DateTimeFormatter;
	import flash.globalization.LocaleID;
	import flash.net.FileReference;
	
	/**
	 * 日志输出
	 * @author SiaoLeon
	 */
	public class Log
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		/**
		 * 调试
		 */
		private static const DEBUG:String = "DEBUG";
		
		/**
		 * 信息
		 */
		private static const INFO:String = "INFO";
		
		/**
		 * 警告
		 */
		private static const WARNING:String = "WARNING";
		
		/**
		 * 错误
		 */
		private static const ERROR:String = "ERROR";
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		private static var _instance:Log;
		
		/**
		 * 获取单例对象
		 */
		private static function get instance():Log
		{
			if (_instance == null)
			{
				_instance = new Log();
			}
			return _instance;
		}
		
		/**
		 * 获取所有日志消息
		 * @return
		 */
		public static function get messages():String
		{
			return Log.instance.messages.join("\r\n");
		}
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		/**
		 * 输出调试
		 * @param args
		 */
		public static function debug(... args):void
		{
			Log.instance.track(Log.DEBUG, args.join(" "));
		}
		
		/**
		 * 输出信息
		 * @param args
		 */
		public static function info(... args):void
		{
			Log.instance.track(Log.INFO, args.join(" "));
		}
		
		/**
		 * 输出警告
		 * @param args
		 */
		public static function warning(... args):void
		{
			Log.instance.track(Log.WARNING, args.join(" "));
		}
		
		/**
		 * 输出错误
		 * @param args
		 */
		public static function error(... args):void
		{
			Log.instance.track(Log.ERROR, args.join(" "));
		}
		
		/**
		 * 导出
		 */
		public static function export():void
		{
			var formatter:DateTimeFormatter = new DateTimeFormatter(LocaleID.DEFAULT);
			formatter.setDateTimePattern("yyyy_MM_dd_HH_mm_ss");
			var date:String = formatter.format(new Date());
			
			var file:FileReference = new FileReference();
			file.save(messages, date + ".log");
		}
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		private var messages:Vector.<LogMessage> = new Vector.<LogMessage>();
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数，单例模式
		 */
		public function Log()
		{
			if (_instance != null)
			{
				throw new SingletonError(this);
			}
			_instance = this;
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		private function track(type:String, message:String):void
		{
			var log:LogMessage = new LogMessage(type, message);
			
			if (FlashPlayer.isDebug)
			{
				trace(log);
			}
			
			this.messages.push(log);
		}
	
	}
}

import flash.globalization.DateTimeFormatter;
import flash.globalization.LocaleID;

/**
 * 日志消息
 * @author SiaoLeon
 */
class LogMessage
{
	private var type:String;
	
	private var time:Number;
	
	private var content:String;
	
	/**
	 * 构造函数
	 */
	function LogMessage(type:String, content:String)
	{
		this.type = type;
		this.time = new Date().time;
		this.content = content;
	}
	
	public function toString():String
	{
		var formatter:DateTimeFormatter = new DateTimeFormatter(LocaleID.DEFAULT);
		formatter.setDateTimePattern("yyyy/MM/dd HH:mm:ss");
		var date:Date = new Date(this.time);
		var time:String = formatter.format(date) + "(" + date.milliseconds + ")";
		
		return "[" + this.type.charAt(0) + "] " + time + " " + this.content;
	}

}
